package com.wl.activity

import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.Toast
import com.example.haoyuban111.mubanapplication.R
import com.example.haoyuban111.mubanapplication.help_class.ContextHelper
import com.example.haoyuban111.mubanapplication.model.Url
import com.example.haoyuban111.mubanapplication.ui.view.DividerItemDecoration
import com.wl.adapter.AdapterPopularLocationKotlin
import com.wl.model.SecondModelKotlin

@Suppress("UNUSED_EXPRESSION")
class MainActivity : AppCompatActivity() {
    private val MARGIN = 20

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val recylerView = findViewById(R.id.recyclerView) as RecyclerView
        val layoutManager = GridLayoutManager(ContextHelper.getApplicationContext(), 2)
        val decoration = DividerItemDecoration(ContextHelper.getApplicationContext(), R.drawable.divider, R.color.transparent)
        decoration.setSize(MARGIN)
        decoration.setType(DividerItemDecoration.BORDER)
        recylerView.addItemDecoration(decoration)
        recylerView.layoutManager = layoutManager
        val mAdapter = AdapterPopularLocationKotlin(mContext = ContextHelper.getApplicationContext())
        mAdapter.setData(getSecondModelsKotlin() as MutableList<SecondModelKotlin>)
        recylerView.adapter = mAdapter
        recylerView.setOnClickListener {
            toast("Hello")
        }

//        mAdapter.setOnItemClickListener()


        val list: MutableList<SecondModelKotlin>? = getNames() as MutableList<SecondModelKotlin>?//?允许非空,不加?的话，一直不为空
        val datas: MutableList<SecondModelKotlin> = ArrayList()
        var model: SecondModelKotlin
        if (list != null) {
//            for (i in list.indices) {
//                model = list[i]
//                model.name = "Liszt" + i
//                datas.add(model)
//            }
            for (i in list) {
                model = i
                model.name = "Liszt" + list.indexOf(model)
                datas.add(model)
            }
            mAdapter.setData(datas)
        } else {
            mAdapter.setData(getSecondModelsKotlin() as MutableList<SecondModelKotlin>)
        }

            print(max(2,6))
    }

    fun Context.toast(message:String, length : Int = Toast.LENGTH_SHORT){
        Toast.makeText(this,message,length)
    }

    fun max(a: Int, b: Int) = if (a > b) a else b//fun 方法，如果a>b，return a ,else return b;

    fun getSecondModelsKotlin(): List<SecondModelKotlin> {
        val list = ArrayList<SecondModelKotlin>()

        val model1 = SecondModelKotlin()
        model1.text = "Hello 1"
        model1.avatarUrl = Url.IMAGE_URL_FRANCE_1
        model1.name = "Marks 1"
        list.add(model1)

        val model2 = SecondModelKotlin()
        model2.text = "Hello 2"
        model2.avatarUrl = Url.IMAGE_URL_FRANCE_2
        model2.name = "Marks 2"
        list.add(model2)

        val model3 = SecondModelKotlin()
        model3.text = "Hello 3"
        model3.avatarUrl = Url.IMAGE_URL_FRANCE_3
        model3.name = "Marks 3"
        list.add(model3)

        val model4 = SecondModelKotlin()
        model4.text = "Hello 4"
        model4.avatarUrl = Url.IMAGE_URL_FRANCE_4
        model4.name = "Marks 4"
        list.add(model4)

        val model5 = SecondModelKotlin()
        model5.text = "Hello 5"
        model5.avatarUrl = Url.IMAGE_URL_FRANCE_2
        model5.name = "Marks 5"
        list.add(model5)

        val model6 = SecondModelKotlin()
        model6.text = "Hello 1"
        model6.avatarUrl = Url.IMAGE_URL_FRANCE_3
        model6.name = "Marks 1"
        list.add(model6)

        val model7 = SecondModelKotlin()
        model7.text = "Hello 2"
        model7.avatarUrl = Url.IMAGE_URL_FRANCE_4
        model7.name = "Marks 2"
        list.add(model7)

        val model8 = SecondModelKotlin()
        model8.text = "Hello 3"
        model8.avatarUrl = Url.IMAGE_URL_FRANCE_1
        model8.name = "Marks 3"
        list.add(model8)

        val model9 = SecondModelKotlin()
        model9.text = "Hello 4"
        model9.avatarUrl = Url.IMAGE_URL_FRANCE_2
        model9.name = "Marks 4"
        list.add(model9)

        val model10 = SecondModelKotlin()
        model10.text = "Hello 5"
        model10.avatarUrl = Url.IMAGE_URL_FRANCE_4
        model10.name = "Marks 5"
        list.add(model10)

        return list

    }

    fun getNames(): List<SecondModelKotlin>? {
        var model: SecondModelKotlin
        val list = java.util.ArrayList<SecondModelKotlin>()
        for (i in 0..49) {
            model = SecondModelKotlin()
            model.name = "hello-" + i
            list.add(model)
        }
//        return list
        return null
    }

}
